﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using WebHttpApi.WebHttpApi.Core;

namespace WebHttpApi.WebHttpApi.HttpApi
{
    /// <summary>
    /// TYComms 的摘要说明：内部指令指令请求接口处理类：
    /// ㈠、水表：
    ///     ⑴、自主产品：NB、GPRS、DTU；
    ///     ⑵、第三方产品：GPRS、LoRa；
    ///     ⑶、对接接入：珠华（停用）。
    /// ㈡、设备：
    ///     ⑴、RTU：
    ///         ①自主产品：RTU；
    ///         ②第三方产品：RTU、DTU。
    ///     ⑵、集中器：
    ///         ①、第三方产品：安美通。
    /// </summary>
    public class TYComms
    {
        public static string result(string DeviceId, string MeterId, string Type, string CommPam)
        {
            string str_result = "";

            if (SelectInMeterId(MeterId, Type))
            {
                //水表
                if (MeterId != "")
                {
                    string sql = "select ybdz,xylx,qybh,sbdz,jcdbh from rp_meter_bz where ybdz='" +
                                    MeterId + "'";

                    System.Data.DataTable Meter_Data = WebSdkCore.select(sql);

                    if (Meter_Data.Rows.Count > 0)
                    {
                        string Meter_xylx = Meter_Data.Rows[0]["xylx"].ToString();
                        string Meter_qybh = Meter_Data.Rows[0]["qybh"].ToString();   //未使用（未进行判断）

                        string Merer_sbdz = Meter_Data.Rows[0]["sbdz"].ToString();
                        string Merer_jcdbh = Meter_Data.Rows[0]["jcdbh"].ToString();

                        string str_msg="";

                        switch (Meter_xylx)
                        {
                            //NB表
                            //无阀
                            case "0":
                            case "2":
                            case "5":
                            case "8":
                            //阀控
                            case "1":
                            case "3":
                            case "6":
                            case "9":
                            //蓝牙
                            case "4":
                            case "7":
                            case "10":
                            //IC卡NB水表
                            case "13":
                            case "14":
                            case "15":
                                {
                                    str_msg = TyNB.Nb_Comm(Meter_xylx, MeterId, Type, CommPam);

                                    //返回
                                    if (str_msg == "成功")
                                    {
                                        var json_result = new
                                        {
                                            status = "success",
                                            message = str_msg,
                                        };
                                        str_result = WebSdkCore.ToJSON(json_result);
                                    }
                                    else
                                    {
                                        var json_result = new
                                        {
                                            status = "error",
                                            message = str_msg,
                                        };
                                        str_result = WebSdkCore.ToJSON(json_result);
                                    }

                                    break;
                                }

                            //GPRS表
                            case "11":
                            case "12":
                                {
                                    str_msg = TyGPRS.GPRS_Comm(Meter_xylx, MeterId, Type, CommPam);

                                    //返回
                                    if (str_msg == "成功")
                                    {
                                        var json_result = new
                                        {
                                            status = "success",
                                            message = str_msg,
                                        };
                                        str_result = WebSdkCore.ToJSON(json_result);
                                    }
                                    else
                                    {
                                        var json_result = new
                                        {
                                            status = "error",
                                            message = str_msg,
                                        };
                                        str_result = WebSdkCore.ToJSON(json_result);
                                    }

                                    break;
                                }
                                ///电信
                            case "16":
                                str_msg = TyNB.Nb_Comm(Meter_xylx, MeterId, Type, CommPam);

                                //返回
                                if (str_msg == "成功")
                                {
                                    var json_result = new
                                    {
                                        status = "success",
                                        message = str_msg,
                                    };
                                    str_result = WebSdkCore.ToJSON(json_result);
                                }
                                else
                                {
                                    var json_result = new
                                    {
                                        status = "error",
                                        message = str_msg,
                                    };
                                    str_result = WebSdkCore.ToJSON(json_result);
                                }

                                break;

                            //移动
                            case "17":
                                str_msg = TyNB.Nb_Comm(Meter_xylx, MeterId, Type, CommPam);

                                //返回
                                if (str_msg == "成功")
                                {
                                    var json_result = new
                                    {
                                        status = "success",
                                        message = str_msg,
                                    };
                                    str_result = WebSdkCore.ToJSON(json_result);
                                }
                                else
                                {
                                    var json_result = new
                                    {
                                        status = "error",
                                        message = str_msg,
                                    };
                                    str_result = WebSdkCore.ToJSON(json_result);
                                }

                                break;
                            case "18":
                                str_msg = TyNB.Nb_Comm(Meter_xylx, MeterId, Type, CommPam);

                                //返回
                                if (str_msg == "成功")
                                {
                                    var json_result = new
                                    {
                                        status = "success",
                                        message = str_msg,
                                    };
                                    str_result = WebSdkCore.ToJSON(json_result);
                                }
                                else
                                {
                                    var json_result = new
                                    {
                                        status = "error",
                                        message = str_msg,
                                    };
                                    str_result = WebSdkCore.ToJSON(json_result);
                                }

                                break;






                            //LORA表 - 腾越：对应 安美通-集中器
                            case "51":
                            case "52":
                            case "53":
                                {
                                    if (Merer_sbdz != "" && Merer_jcdbh != "")
                                    {
                                        Merer_jcdbh = "0000" + Merer_jcdbh;
                                        Merer_jcdbh = Merer_jcdbh.Substring(Merer_jcdbh.Length - 4);

                                        str_msg = TyLora.Lora_Comm(Merer_sbdz, Merer_jcdbh, Meter_xylx, MeterId, Type, CommPam); 

                                        //返回
                                        if (str_msg == "成功")
                                        {
                                            var json_result = new
                                            {
                                                status = "success",
                                                message = str_msg,
                                            };
                                            str_result = WebSdkCore.ToJSON(json_result);
                                        }
                                        else
                                        {
                                            var json_result = new
                                            {
                                                status = "error",
                                                message = str_msg,
                                            };
                                            str_result = WebSdkCore.ToJSON(json_result);
                                        }
                                    }
                                    else
                                    {
                                        //参数错误
                                        var json_result = new
                                        {
                                            status = "error",
                                            message = "仪表信息不完整",
                                        };
                                        str_result = WebSdkCore.ToJSON(json_result);
                                    }

                                    break;
                                }

                            //2021.04.09
                            //LORA表 - 济宁天成：对应 济宁天成-集中器
                            case "81":
                            case "82":
                            case "83":
                                {
                                    if (Merer_sbdz != "")
                                    {
                                        str_msg = TcLora.Lora_Comm(Merer_sbdz, Meter_xylx, MeterId, Type, CommPam);

                                        //返回
                                        if (str_msg == "成功")
                                        {
                                            var json_result = new
                                            {
                                                status = "success",
                                                message = str_msg,
                                            };
                                            str_result = WebSdkCore.ToJSON(json_result);
                                        }
                                        else
                                        {
                                            var json_result = new
                                            {
                                                status = "error",
                                                message = str_msg,
                                            };
                                            str_result = WebSdkCore.ToJSON(json_result);
                                        }
                                    }
                                    else
                                    {
                                        //参数错误
                                        var json_result = new
                                        {
                                            status = "error",
                                            message = "仪表信息不完整",
                                        };
                                        str_result = WebSdkCore.ToJSON(json_result);
                                    }

                                    break;
                                }

                            //对接推送
                            case "101":
                                {
                                    //获取水表抄表数据
                                    //str_msg = ZhGetData.result(MeterId);
                                    
                                    ////2020-07-20 暂停业务
                                    ////下发阀控指令
                                    //str_msg = ZhValueComm.result(MeterId, Type, CommPam);

                                    ////返回
                                    //var json_result = new
                                    //{
                                    //    status = "success",
                                    //    message = str_msg,
                                    //};
                                    //str_result = WebSdkCore.ToJSON(json_result);

                                    break;
                                }

                            //RTU + 超声水表 数据监控：控阀
                            case "201":
                            case "202":
                                {
                                    string Device_sql = "select sbdz,sblx from rp_device_bz where sbdz='" +
                                                    Merer_sbdz + "'";

                                    System.Data.DataTable Device_Data = WebSdkCore.select(Device_sql);

                                    if (Device_Data.Rows.Count > 0)
                                    {
                                        string Device_xylx = Device_Data.Rows[0]["sblx"].ToString();

                                        str_msg = TyRTU.Rtu_Comm(Device_xylx, MeterId, Merer_sbdz, "7", CommPam);

                                        //返回
                                        if (str_msg == "成功")
                                        {
                                            var json_result = new
                                            {
                                                status = "success",
                                                message = str_msg,
                                            };
                                            str_result = WebSdkCore.ToJSON(json_result);
                                        }
                                        else
                                        {
                                            var json_result = new
                                            {
                                                status = "error",
                                                message = str_msg,
                                            };
                                            str_result = WebSdkCore.ToJSON(json_result);
                                        }
                                    }
                                    else
                                    {
                                        var json_result = new
                                        {
                                            status = "error",
                                            message = "设备未报装",
                                        };
                                        str_result = WebSdkCore.ToJSON(json_result);
                                    }

                                    break;
                                }
                            case "244":
                                  str_msg = TyNB.Nb_Comm(Meter_xylx, MeterId, Type, CommPam);

                                //返回
                                if (str_msg == "成功")
                                {
                                    var json_result = new
                                    {
                                        status = "success",
                                        message = str_msg,
                                    };
                                    str_result = WebSdkCore.ToJSON(json_result);
                                }
                                else
                                {
                                    var json_result = new
                                    {
                                        status = "error",
                                        message = str_msg,
                                    };
                                    str_result = WebSdkCore.ToJSON(json_result);
                                }

                                break;

                        }
                    }
                    else
                    {
                        //参数错误
                        var json_result = new
                        {
                            status = "error",
                            message = "仪表未报装",
                        };
                        str_result = WebSdkCore.ToJSON(json_result);
                    }
                }
            }           
            else if (SelectInDeviceId(DeviceId, Type))
            {
                //RTU、集中器
                if (DeviceId != "")
                {
                    string sql = "select sbdz,sblx from rp_device_bz where sbdz='" +
                                    DeviceId + "'";

                    System.Data.DataTable Device_Data = WebSdkCore.select(sql);

                    if (Device_Data.Rows.Count > 0)
                    {
                        string Device_xylx = Device_Data.Rows[0]["sblx"].ToString();

                        string str_msg = "";

                        switch (Device_xylx)
                        {
                            //安美通集中器
                            case "0":
                                {
                                    str_msg = TyJzq.Jzq_Comm(Device_xylx, DeviceId, Type, CommPam);

                                    //返回
                                    if (str_msg == "成功")
                                    {
                                        var json_result = new
                                        {
                                            status = "success",
                                            message = str_msg,
                                        };
                                        str_result = WebSdkCore.ToJSON(json_result);
                                    }
                                    else
                                    {
                                        var json_result = new
                                        {
                                            status = "error",
                                            message = str_msg,
                                        };
                                        str_result = WebSdkCore.ToJSON(json_result);
                                    }

                                    break;
                                }

                            //RTU
                            case "1":
                            case "2":
                                {
                                    //查询规划表号，没有规划默认为“00000000000000”
                                    string Device_MeterId = "";

                                    sql = "select ybdz from v_rp_meter_bz where sbdz='" +
                                                    DeviceId + "'";

                                    Device_Data = WebSdkCore.select(sql);

                                    if (Device_Data.Rows.Count > 0)
                                    {
                                        Device_MeterId = Device_Data.Rows[0]["ybdz"].ToString();
                                    }
                                    else
                                    {
                                        Device_MeterId = "00000000000000";
                                    }

                                    str_msg = TyRTU.Rtu_Comm(Device_xylx, Device_MeterId, DeviceId, Type, CommPam);

                                    //返回
                                    if (str_msg == "成功")
                                    {
                                        var json_result = new
                                        {
                                            status = "success",
                                            message = str_msg,
                                        };
                                        str_result = WebSdkCore.ToJSON(json_result);
                                    }
                                    else
                                    {
                                        var json_result = new
                                        {
                                            status = "error",
                                            message = str_msg,
                                        };
                                        str_result = WebSdkCore.ToJSON(json_result);
                                    }

                                    break;
                                }
                        }
                    }
                    else
                    {
                        //参数错误
                        var json_result = new
                        {
                            status = "error",
                            message = "设备未报装",
                        };
                        str_result = WebSdkCore.ToJSON(json_result);
                    }
                }
            }
            else
            {
                //参数错误
                var json_result = new
                {
                    status = "error",
                    message = "参数错误",
                };
                str_result = WebSdkCore.ToJSON(json_result);
            }
            return str_result;
        }

        public static bool SelectInDeviceId(string DeviceId, string Type)
        {
            bool result;
            if (string.IsNullOrEmpty(DeviceId) || string.IsNullOrEmpty(Type))
            {
                result = false;
            }
            else
            {
                result = true;
            }
            return result;
        }

        public static bool SelectInMeterId(string MeterId, string Type)
        {
            bool result;
            if (string.IsNullOrEmpty(MeterId) || string.IsNullOrEmpty(Type))
            {
                result = false;
            }
            else
            {
                result = true;
            }
            return result;
        }
    }
}